{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6d96e659-f743-40cb-bb49-d916a215aa4f",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Real-World Case Studies - Equity Options"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bfc0241-b312-45cd-b1b6-cfecab9ad85e",
   "metadata": {},
   "source": [
    "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d0d0add-ff03-4c37-af3c-3942c8adc582",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Load the Libs we need"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4b0d19b3-7892-470c-8051-b3460605ed1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import Lib\n",
    "import pandas as pd\n",
    "import datetime as dt\n",
    "import pytz\n",
    "import os\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import yfinance as yf\n",
    "import scipy.stats as si\n",
    "import math\n",
    "import networkx as nx\n",
    "\n",
    "# import module\n",
    "from datetime import datetime, timezone\n",
    "from datetime import date, time\n",
    "from math import trunc\n",
    "from dateutil.parser import parse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36335255-3a12-4108-b282-d524a5e889cb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a3ac8aa6-a494-406c-b86f-e3227c0c28c4",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Introduction to Real-World Equity Options Case Studies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cd82d377-850f-44c6-810a-ca74c3283cab",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The price of the European Call Option for AAPL is: 4.9863\n",
      "The price of the European Put Option for AAPL is: 13.7688\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import scipy.stats as si\n",
    "\n",
    "def equity_option_pricing(S, K, T, r, q, vol, option_type=\"call\"):\n",
    "    # S: Current stock price (e.g., AAPL's current price)\n",
    "    # K: Option strike price\n",
    "    # T: Time to expiration\n",
    "    # r: Risk-free interest rate\n",
    "    # q: Continuous dividend yield\n",
    "    # vol: Volatility of the underlying stock\n",
    "    d1 = (np.log(S / K) + (r - q + 0.5 * vol ** 2) * T) / (vol * np.sqrt(T))\n",
    "    d2 = d1 - vol * np.sqrt(T)\n",
    "    if option_type == \"call\":\n",
    "        option_price = (S * np.exp(-q * T) * si.norm.cdf(d1, 0.0, 1.0) - K * np.exp(-r * T) * si.norm.cdf(d2, 0.0, 1.0))\n",
    "    elif option_type == \"put\":\n",
    "        option_price = (K * np.exp(-r * T) * si.norm.cdf(-d2, 0.0, 1.0) - S * np.exp(-q * T) * si.norm.cdf(-d1, 0.0, 1.0))\n",
    "    return option_price\n",
    "\n",
    "# Example usage\n",
    "S = 150  # Current AAPL stock price\n",
    "K = 160  # Strike Price\n",
    "T = 0.5  # Time to expiration in years\n",
    "r = 0.02  # Risk-free interest rate\n",
    "q = 0.005  # Dividend yield for AAPL\n",
    "vol = 0.20  # Volatility of AAPL stock\n",
    "\n",
    "call_price = equity_option_pricing(S, K, T, r, q, vol, option_type=\"call\")\n",
    "put_price = equity_option_pricing(S, K, T, r, q, vol, option_type=\"put\")\n",
    "\n",
    "print(f\"The price of the European Call Option for AAPL is: {call_price:.4f}\")\n",
    "print(f\"The price of the European Put Option for AAPL is: {put_price:.4f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "669a7722-961e-4095-bc90-2affc5c134e0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "bbbd8cbb-8223-41bd-87fc-5a41170fef43",
   "metadata": {},
   "source": [
    "## Equity Options Hedging Strategies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "49e2d97f-ba68-4d2f-8584-75a2ca61b784",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The delta of the European Call Option for AAPL is: 0.4499\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import scipy.stats as si\n",
    "\n",
    "def delta(S, K, T, r, q, vol, option_type=\"call\"):\n",
    "    d1 = (np.log(S / K) + (r - q + 0.5 * vol ** 2) * T) / (vol * np.sqrt(T))\n",
    "    \n",
    "    if option_type == \"call\":\n",
    "        return si.norm.cdf(d1, 0.0, 1.0)  # No need to multiply by exp(-q*T) for equity options as it's integrated into the formula\n",
    "    elif option_type == \"put\":\n",
    "        return si.norm.cdf(d1, 0.0, 1.0) - 1  # Adjusted for equity options\n",
    "\n",
    "# Example usage\n",
    "S = 150  # Current stock price of AAPL\n",
    "K = 155  # Strike Price\n",
    "T = 0.5  # Time to expiration in years\n",
    "r = 0.02  # Risk-free interest rate\n",
    "q = 0.01  # Dividend yield for AAPL\n",
    "vol = 0.20  # Volatility\n",
    "\n",
    "delta_call = delta(S, K, T, r, q, vol, option_type=\"call\")\n",
    "\n",
    "print(f\"The delta of the European Call Option for AAPL is: {delta_call:.4f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e17d2bf2-aecd-4ac6-9123-1d91821c9bc8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ddf80c7b-f21f-49ff-b730-9e430f5bff6f",
   "metadata": {},
   "source": [
    "## Speculative Trading in Equity Options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "15ce8d38-ef69-4137-9332-98365f535405",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The profit from the long call position on AAPL is: 2.4693\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import scipy.stats as si\n",
    "\n",
    "# Pricing function for a dividend-paying stock\n",
    "def equity_option_pricing(S, K, T, r, q, vol, option_type=\"call\"):\n",
    "    d1 = (np.log(S / K) + (r - q + 0.5 * vol ** 2) * T) / (vol * np.sqrt(T))\n",
    "    d2 = d1 - vol * np.sqrt(T)\n",
    "    \n",
    "    if option_type == \"call\":\n",
    "        return (S * np.exp(-q * T) * si.norm.cdf(d1, 0.0, 1.0) - K * np.exp(-r * T) * si.norm.cdf(d2, 0.0, 1.0))\n",
    "    elif option_type == \"put\":\n",
    "        return (K * np.exp(-r * T) * si.norm.cdf(-d2, 0.0, 1.0) - S * np.exp(-q * T) * si.norm.cdf(-d1, 0.0, 1.0))\n",
    "\n",
    "# Given parameters (make sure to set them based on your needs)\n",
    "S = 150  # Current stock price of AAPL\n",
    "K = 155  # Strike Price\n",
    "T = 0.5  # Time to expiration in years\n",
    "r = 0.02  # Risk-free interest rate\n",
    "q = 0.01  # Dividend yield for AAPL\n",
    "vol = 0.20  # Volatility\n",
    "\n",
    "call_price = equity_option_pricing(S, K, T, r, q, vol, option_type=\"call\")\n",
    "\n",
    "# Assuming the AAPL stock price goes up to 155\n",
    "S_new = 155\n",
    "\n",
    "# New call price\n",
    "new_call_price = equity_option_pricing(S_new, K, T, r, q, vol, option_type=\"call\")\n",
    "\n",
    "# Profit from the long call position\n",
    "profit = new_call_price - call_price\n",
    "\n",
    "print(f\"The profit from the long call position on AAPL is: {profit:.4f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "518866a3-5790-4e48-aadf-238b68d7d479",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "29c67e50-7a51-49b9-8385-f8fd2d5d4891",
   "metadata": {},
   "source": [
    "## Risk Management in Equity Options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ae0fcb44-0610-43ad-868a-909f83dfb0a3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Value at Risk (VaR) of the AAPL call options portfolio, considering dividends, is: $2925.84\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import scipy.stats as si\n",
    "\n",
    "# Pricing function for a dividend-paying stock\n",
    "def equity_option_pricing(S, K, T, r, q, vol, option_type=\"call\"):\n",
    "    d1 = (np.log(S / K) + (r - q + 0.5 * vol ** 2) * T) / (vol * np.sqrt(T))\n",
    "    d2 = d1 - vol * np.sqrt(T)\n",
    "    \n",
    "    if option_type == \"call\":\n",
    "        return (S * np.exp(-q * T) * si.norm.cdf(d1, 0.0, 1.0) - K * np.exp(-r * T) * si.norm.cdf(d2, 0.0, 1.0))\n",
    "    elif option_type == \"put\":\n",
    "        return (K * np.exp(-r * T) * si.norm.cdf(-d2, 0.0, 1.0) - S * np.exp(-q * T) * si.norm.cdf(-d1, 0.0, 1.0))\n",
    "\n",
    "# Given parameters (make sure to set them based on your needs)\n",
    "S = 150  # Current stock price of AAPL\n",
    "K = 155  # Strike Price\n",
    "T = 0.5  # Time to expiration in years\n",
    "r = 0.02  # Risk-free interest rate\n",
    "q = 0.01  # Dividend yield for AAPL\n",
    "vol = 0.20  # Volatility\n",
    "\n",
    "call_price = equity_option_pricing(S, K, T, r, q, vol, option_type=\"call\")\n",
    "\n",
    "# Assume a portfolio of 1000 European Call Options\n",
    "portfolio_size = 1000\n",
    "portfolio_value = portfolio_size * call_price\n",
    "\n",
    "# Assume a confidence level of 99%\n",
    "alpha = 0.99\n",
    "\n",
    "# Simplified VaR Calculation for Options Portfolio \n",
    "VaR = portfolio_value * (1 - si.norm.cdf(vol * np.sqrt(T), 0.0, 1.0))\n",
    "\n",
    "print(f\"The Value at Risk (VaR) of the AAPL call options portfolio, considering dividends, is: ${VaR:.2f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d0de498-9d02-4cf0-8204-1f838e6973ae",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ecf1ffce-a612-4050-b4d7-da08c5bdd243",
   "metadata": {},
   "source": [
    "## Implementing Pricing and Valuation Models in Equity Options"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "04944328-dfc2-4488-a6f6-1f0acd560e40",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The price of the AAPL European Call Option with recalibrated volatility, considering dividends, is: $5.76\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import scipy.stats as si\n",
    "\n",
    "# Pricing function for a dividend-paying stock\n",
    "def equity_option_pricing(S, K, T, r, q, vol, option_type=\"call\"):\n",
    "    d1 = (np.log(S / K) + (r - q + 0.5 * vol ** 2) * T) / (vol * np.sqrt(T))\n",
    "    d2 = d1 - vol * np.sqrt(T)\n",
    "    \n",
    "    if option_type == \"call\":\n",
    "        return (S * np.exp(-q * T) * si.norm.cdf(d1, 0.0, 1.0) - K * np.exp(-r * T) * si.norm.cdf(d2, 0.0, 1.0))\n",
    "    elif option_type == \"put\":\n",
    "        return (K * np.exp(-r * T) * si.norm.cdf(-d2, 0.0, 1.0) - S * np.exp(-q * T) * si.norm.cdf(-d1, 0.0, 1.0))\n",
    "\n",
    "# Given parameters (these should be set based on your needs or prior data)\n",
    "S = 150  # Current stock price of AAPL\n",
    "K = 155  # Strike Price\n",
    "T = 0.5  # Time to expiration in years\n",
    "r = 0.02  # Risk-free interest rate\n",
    "q = 0.01  # Dividend yield for AAPL\n",
    "vol = 0.20  # Original Volatility\n",
    "\n",
    "# Assuming a new recalibrated volatility\n",
    "new_vol = 0.18\n",
    "\n",
    "# New call price with recalibrated volatility\n",
    "new_call_price_recalibrated = equity_option_pricing(S, K, T, r, q, new_vol, option_type=\"call\")\n",
    "\n",
    "print(f\"The price of the AAPL European Call Option with recalibrated volatility, considering dividends, is: ${new_call_price_recalibrated:.2f}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "156c7f93-6503-49d2-ba32-b2fc74580114",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
